package com.jackrain.nea.ad.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.ad.api.AdactionSaveCmd;
import com.jackrain.nea.ad.mapper.AdactionMapper;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.core.db.Tools;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * AdactionSaveCmdImpl class
 *
 * @author bigzhi
 * @date 2017/11/22
 */

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdactionSaveCmdImpl extends CommandAdapter implements AdactionSaveCmd {

    @Override
    public ValueHolder execute(QuerySession querySession) throws NDSException {
        ValueHolder vh = new ValueHolder();
        AdactionMapper adactionMapper= ApplicationContextHandle.getBean(AdactionMapper.class);
        DefaultWebEvent event = querySession.getEvent();

        //操作员ID、NAME、ENAME
        Long userId = Integer.valueOf(querySession.getUser().getId()).longValue();
        String userName = querySession.getUser().getName();
        String userEname = querySession.getUser().getEname();

        //系统当前时间
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        //TODO 获取参数
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(event.getParameterValue("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        Long objid = param.getLong("objid");
        JSONObject fixColumn = param.getJSONObject("fixcolumn");
        JSONObject jo = null;
        if(fixColumn != null){
            jo = fixColumn.getJSONObject("AD_ACTION");
        }

        if(objid > 0){
            //TODO 更新操作

            if(jo != null){
                //记录不存在，报错
                if (adactionMapper.findCountById(objid) == 0) {
                    vh.put("code", -1);
                    vh.put("message", Resources.getMessage("当前记录已不存在！", querySession.getLocale()));
                    return vh;
                }
            }

            //添加更新参数
            jo.put("MODIFIERID", userId);
            jo.put("MODIFIERNAME", userName);
            jo.put("MODIFIERENAME", userEname);
            jo.put("MODIFIEDDATE", timestamp);
            jo.put("ID",objid);

            adactionMapper.update(jo);

        }else{
            //TODO 新增操作

            if(jo != null){

                JSONObject actionEntity = new JSONObject();
                objid = Tools.getSequence("AD_ACTION");

                actionEntity.put("ID", objid);
                actionEntity.put("OWNERID", userId);
                actionEntity.put("OWNERNAME", userName);
                actionEntity.put("OWNERENAME", userEname);
                actionEntity.put("CREATIONDATE", timestamp);
                actionEntity.put("MODIFIERID", userId);
                actionEntity.put("MODIFIERNAME", userName);
                actionEntity.put("MODIFIERENAME", userEname);
                actionEntity.put("MODIFIEDDATE", timestamp);
                actionEntity.putAll(jo);

                adactionMapper.insert(actionEntity);

                //添加结果信息
                JSONObject data = new JSONObject();
                data.put("objid", objid);
                data.put("tablename", "AD_ACTION");
                vh.put("data", data);
            }
        }

        vh.put("code",0);
        vh.put("message", Resources.getMessage("成功", querySession.getLocale()));
        return vh;
    }

}
